"""
class TestEmp(unittest.TestCase):
    def test_emp(self):
        # 1. 删除指定手机号的记录
        # 2. 接口测试
        # 3. 删除指定手机号的记录
"""
import unittest

import requests

from db_util import DBUtil
from login_api import LoginApi


class EmpApi(object):
    def __init__(self):
        self.emp_url = 'http://ihrm-test.itheima.net/api/sys/user'

    def emp_add_api(self, body, headers):
        return requests.post(self.emp_url, json=body, headers=headers)

    def emp_query_api(self, path_param, headers):
        return requests.get(self.emp_url + '/' + path_param, headers=headers)

    def emp_modify_api(self, path_param, body, headers):
        return requests.put(self.emp_url + '/' + path_param, json=body, headers=headers)

    def emp_delete_api(self, path_param, headers):
        return requests.delete(self.emp_url + '/' + path_param, headers=headers)


class TestEmpApi(unittest.TestCase):
    def test_emp(self):
        # 删除13620920013手机号信息
        mobile = 13620920013

        sql = f"delete from bs_user where mobile='{mobile}'"
        DBUtil.iud_data(sql)

        # 添加员工
        emp_add_body = {
            "username": "xjchen013",
            "mobile": mobile,
            "workNumber": "0013"
        }

        # 获取token，封装请求头
        headers = LoginApi().get_au_headers()

        resp_add = EmpApi().emp_add_api(emp_add_body, headers)
        resp_add_data = resp_add.json()
        print('resp_add_data:', resp_add_data)

        self.assertEqual(200, resp_add.status_code)
        self.assertEqual(True, resp_add_data.get('success'))
        self.assertEqual(10000, resp_add_data.get('code'))
        self.assertIn('成功', resp_add_data.get('message'))

        # 查询员工
        emp_id = resp_add_data.get('data').get('id')
        resp_query = EmpApi().emp_query_api(emp_id, headers)
        resp_query_data = resp_query.json()
        print('resp_query_data:', resp_query_data)

        self.assertEqual(200, resp_add.status_code)
        self.assertEqual(True, resp_add_data.get('success'))
        self.assertEqual(10000, resp_add_data.get('code'))
        self.assertIn('成功', resp_add_data.get('message'))

        # 修改员工
        modify_body = {
            "username": "xjchen_000000001"
        }
        resp_modify = EmpApi().emp_modify_api(emp_id, modify_body, headers)
        resp_modify_data = resp_modify.json()
        print('resp_modify_data:', resp_modify_data)

        self.assertEqual(200, resp_modify.status_code)
        self.assertEqual(True, resp_modify_data.get('success'))
        self.assertEqual(10000, resp_modify_data.get('code'))
        self.assertIn('成功', resp_modify_data.get('message'))

        # 删除员工
        resp_delete = EmpApi().emp_delete_api(emp_id, headers)
        resp_delete_data = resp_delete.json()
        print('resp_delete_data:', resp_delete_data)

        self.assertEqual(200, resp_delete.status_code)
        self.assertEqual(True, resp_delete_data.get('success'))
        self.assertEqual(10000, resp_delete_data.get('code'))
        self.assertIn('成功', resp_delete_data.get('message'))